﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace IndyMosaic.Core.Extensions
{
    public static class RequestExtensions
    {

        public static string GetRelativeUrl(this HttpRequest request, string applicationUrl)
        {
            string appPath = request.ApplicationPath;
            string relativeUrl = string.Format("~{0}", applicationUrl.Substring(appPath.Length));
            return relativeUrl;
        }

        public static string GetContentAbsoluteUrl(this HttpRequest request, System.Web.UI.Control control, string relativeUrl)
        {
            if (relativeUrl.StartsWith("~/"))
            {
                return request.Url.GetLeftPart(UriPartial.Authority) + control.ResolveUrl(relativeUrl);
            }

            Uri uri = new Uri(relativeUrl);
            if (!uri.IsAbsoluteUri)
            {
                string first = request.Url.GetLeftPart(UriPartial.Authority);
                if (!relativeUrl.StartsWith(request.ApplicationPath))
                {
                    first += request.ApplicationPath;
                }
                return first + relativeUrl;
            }
            else
            {
                return relativeUrl;
            }
        }
    }
}
